<template>
	<view class="bg-white padding-lr">
		<u-form :model="form" ref="uForm" label-width="auto">
			<u-form-item label="登录名" prop="loginName" required>
				<u-input v-model="form.loginName" />
			</u-form-item>
			<u-form-item label="密码" prop="password">
				<u-input v-model="form.password" />
			</u-form-item>
			<u-form-item label="姓名" prop="realName">
				<u-input v-model="form.realName" />
			</u-form-item>
			<u-form-item label="昵称" prop="nickName">
				<u-input v-model="form.nickName" />
			</u-form-item>
			<u-form-item label="职务" prop="position">
				<u-input v-model="form.position" />
			</u-form-item>
			<u-form-item label="性别" prop="sex">
				<u-radio-group v-model="form.sex">
					<u-radio name="M">男</u-radio>
					<u-radio name="F">女</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="手机号" prop="mobile">
				<u-input v-model="form.mobile" />
			</u-form-item>
			<u-form-item label="用户编码" prop="userCode">
				<u-input v-model="form.userCode" />
			</u-form-item>
			<u-form-item label="身份证号" prop="idCode">
				<u-input v-model="form.idCode" />
			</u-form-item>
			<u-form-item label="电子邮箱" prop="email">
				<u-input v-model="form.email" />
			</u-form-item>
			<u-form-item label="居住地址" prop="address">
				<u-input v-model="form.address" />
			</u-form-item>
			<u-form-item label="用户图像" prop="avatar">
				<u-input v-model="form.avatar" />
			</u-form-item>
			<u-form-item label="用户标记" prop="flag">
				<u-input v-model="form.flag" />
			</u-form-item>
			<u-form-item label="序号" prop="seq" required>
				<u-input type="number" v-model="form.seq" />
			</u-form-item>
			<view class="padding-tb">
				<u-button type="primary" @click="save">保存</u-button>
			</view>
		</u-form>
	</view>
</template>

<script>
	export default {
		onLoad: function(option) {
			if (option.id) {
				this.form.id = option.id;
				this.get();
			} else {
				this.form.orgId = option.orgId || null;
			}
		},
		onShow: function() {},
		onReady: function() {
			this.$refs.uForm.setRules(this.rules);
		},
		onReachBottom: function() {},
		data() {
			return {
				form: {
					id: null,
					loginName: null,
					password: null,
					realName: null,
					nickName: null,
					sex: null,
					position: null,
					mobile: null,
					userCode: null,
					idCode: null,
					email: null,
					address: null,
					avatar: null,
					flag: null,
					orgId: null,
					seq: 1.0
				},
				rules: {
					loginName: [{
						required: true,
						message: "请填写登录名"
					}],
				}
			};
		},
		methods: {
			save() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$http.post(`/admin/sys/rbac/user/save`, this.form, res => {
							const eventChannel = this.getOpenerEventChannel()
							eventChannel.emit('refresh', {});
							this.$utils.modal("提示", "保存成功！", false, flag => {
								if (flag) {
									uni.navigateBack();
								}
							})
						}, false, true, true);
					}
				});
			},
			get() {
				this.$http.get(`/admin/sys/rbac/user/get/${this.form.id}`, {}, res => {
					this.form = res.data || this.form;
					this.form.password = null;
				}, false, true, true);
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>
